package com.tcwlkj.uuService.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.ShareModel;
import com.tcwlkj.uuService.ui.RecommentActivity;
import com.tcwlkj.uuService.utils.EventCenter;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import org.greenrobot.eventbus.EventBus;

import java.net.URLDecoder;
import java.util.List;

/**
 * Created by Administrator on 2017/11/21 0021.
 */

public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder> {


    private List<ShareModel.Children> mMsgList;
    private Activity activity;
    private UMWeb web;

    public ShareAdapter(List<ShareModel.Children> mMsgList,Activity activity) {
        this.mMsgList = mMsgList;
        this.activity=activity;
    }

    public void updateData(List<ShareModel.Children> data) {
        this.mMsgList = data;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.share_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewGroup.LayoutParams lp=holder.ll_share.getLayoutParams();
        lp.width=getScreenWidth()/mMsgList.size();
        holder.ll_share.setLayoutParams(lp);
        holder.tv_share.setText(mMsgList.get(position).getName());
        Glide.with(MyApp.getContext()).load(mMsgList.get(position).getLogo()).error(R.mipmap.personal_touxiang).into(holder.iv_share);

        holder.iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (mMsgList.get(position).getName()){
                    case "微信":
                        web = new UMWeb(mMsgList.get(position).getUrl());
                        web.setTitle(mMsgList.get(position).getTitle());
                        web.setThumb(new UMImage(MyApp.getContext(),mMsgList.get(position).getImages()));
                        web.setDescription(URLDecoder.decode(mMsgList.get(position).getDesc()));
                        new ShareAction(activity)
                                .setPlatform(SHARE_MEDIA.WEIXIN)
                                .withText(URLDecoder.decode(mMsgList.get(position).getDesc()))
                                .withMedia(web)
                                .setCallback(shareListener)
                                .share();
                        break;
                    case "朋友圈":
                        web = new UMWeb(mMsgList.get(position).getUrl());
                        web.setTitle(mMsgList.get(position).getTitle());
                        web.setThumb(new UMImage(MyApp.getContext(),mMsgList.get(position).getImages()));
                        web.setDescription(URLDecoder.decode(mMsgList.get(position).getDesc()));
                        new ShareAction(activity)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                .withText(URLDecoder.decode(mMsgList.get(position).getDesc()))
                                .withMedia(web)
                                .setCallback(shareListener)
                                .share();
                        break;
                    case "短信":
                        EventBus.getDefault().post(new EventCenter<Integer>(1011,position));
                        break;
                }
            }
        });
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
        }
    };
    @Override
    public int getItemCount() {
        return mMsgList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_share;
        ImageView iv_share;
        LinearLayout ll_share;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_share= (TextView) itemView.findViewById(R.id.tv_share);
            iv_share= (ImageView) itemView.findViewById(R.id.iv_share);
            ll_share= (LinearLayout) itemView.findViewById(R.id.ll_share);
        }
    }
    //获取屏幕的宽度
    public int getScreenWidth() {
        Point point = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(point);
        return point.x;
    }
}
